/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dto.LoggedUser;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.DBConnection;

/**
 *
 * @author Shuvo
 */
public class Approval extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String subject = (String) session.getAttribute("subject");
        String quesId = (String) session.getAttribute("quesId");
        session.setAttribute("subject", null);
        session.setAttribute("quesId", null);
        String ans = request.getParameter("ans");
        String status = request.getParameter("review");
        String redirectUrl = "/discuss.jsp?subject="+subject+"&q="+quesId;
        try {
            Connection con = DBConnection.getConn();
            LoggedUser loggedUser = (LoggedUser) session.getAttribute("user");
            if (con != null && loggedUser != null) {
                Statement statement = con.createStatement();
                String query = "update QUESTION set status = '"+status+"', REVIEWED_BY="+loggedUser.getUserId()+" where QUES_ID="+quesId;
                int i = statement.executeUpdate(query);
                if (i == 1) {
                    query = "insert into ANSWER values ("+quesId+", '"+ans+"', "+loggedUser.getUserId()+", to_char(sysdate, 'dd/Mon/yyyy hh:mi:ssam'))";
                    statement.execute(query);
                }
                statement.close();
                con.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + redirectUrl);
    }
}
